package com.necsv;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

import com.necsv.Mylog.Mylog;

public class PreferenceScreenActivity extends PreferenceActivity{
	private static final int MEDIA_IMAGE_REQUEST_CODE = 1;
    private static final int CAMERA_IMAGE_REQUEST_CODE = 2;
	private static final String TAG = "PreferenceScreenActivity";
    public static Uri cImageUri;
    public static Context cContext;
    public static Activity cActivity;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
//		setContentView(R.layout.pre_layout);
		addPreferencesFromResource(R.xml.preference);
		//setContentView(R.layout.tutorial1);
		this.cContext = (Context)this;
        this.cActivity = (Activity)this;
        
        
        
        Preference Pre2 = findPreference("preSc2");
        Pre2.getView(getCurrentFocus(), null).findViewById(R.id.imgFre);
//        Button btn1 = (Button) findViewById(R.id);
//        findViewById(id)
        findViewById(R.id.btnprelayout);
        View v = Pre2.getView(null, null);
        Button btn = (Button)v. findViewById(R.id.btnprelayout);
        btn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Mylog.Toat(getBaseContext(), "Btn Preferences Click111111111111");
			}
		});
        
    
		Preference btnPref = findPreference("btnPref");
		btnPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			
			public boolean onPreferenceClick(Preference preference) {
				// TODO Auto-generated method stub
				Mylog.Toat(getBaseContext(), "Btn Preferences Click");
				imageUploadDialog();
				return false;
			}
		});
	}
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		Log.v("pre", "onStart");
		super.onStart();
		getPrefs();
	}
	
	public void getPrefs(){
		final CheckBoxPreference chbPref = (CheckBoxPreference) findPreference("chbPref");
		chbPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				// TODO Auto-generated method stub
				if(chbPref.isChecked())
				{
					Mylog.Toat(getBaseContext(), "Checked");
				}
				return false;
			}
		});
		
	}
	
	public boolean imageUploadDialog(){
        final CharSequence[] items = {"Take picture now","Upload from gallery"};
        AlertDialog.Builder lAlertDialog = new AlertDialog.Builder(cContext);
        lAlertDialog.setTitle("Upload action");
        lAlertDialog.setCancelable(true);
        lAlertDialog.setItems(items,
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i){
                //Toast.makeText(getApplicationContext(), "Selected item: " +i,  Toast.LENGTH_SHORT).show();
                if (i == 0){
                    attachCameraImage();
                }
                if (i == 1){
                    attachGalleryImage();
                }
            }
        });
        lAlertDialog.setIcon(R.drawable.bol_rood);
        lAlertDialog.show();
        return true;
    }
	
	public void attachGalleryImage(){
        Intent getImageFromGalleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        startActivityForResult(getImageFromGalleryIntent, MEDIA_IMAGE_REQUEST_CODE);
    }

    public void attachCameraImage(){
        String fileName = "testphoto.jpg";
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, fileName);
        values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
        cImageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, cImageUri);
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        startActivityForResult(intent, CAMERA_IMAGE_REQUEST_CODE);
    }
    
    protected final void onActivityResult(final int requestCode, final int resultCode, final Intent i) {
        Log.d(TAG,"--> Received callback with:" + resultCode);
        super.onActivityResult(requestCode, resultCode, i);
        if(resultCode == RESULT_OK) {
            Log.d(TAG,"--> Result OK with:" + requestCode);
            switch(requestCode) {
            case MEDIA_IMAGE_REQUEST_CODE:
                Log.d(TAG,"--> MEDIA_IMAGE_REQUEST_CODE");
//                Gui.GuiProgressDialog.showLoadingSpinner(cActivity);
                cImageUri = i.getData();
                if (cImageUri == null){Log.d(TAG,"--> ImageURI is null!");}
//                Lmt.serviceBinder.serviceThread("uploadMemberPicture");
                Log.v(TAG, "image url:" + cImageUri.toString());
                break;
            case CAMERA_IMAGE_REQUEST_CODE:
                Log.d(TAG,"--> CAMERA_IMAGE_REQUEST_CODE");
                //cImageUri = i.getData();
                if (cImageUri == null){Log.d(TAG,"--> ImageURI is null!");}
//                Lmt.serviceBinder.serviceThread("uploadMemberPicture");
                Log.v(TAG, "image url:" + cImageUri.toString());
                break;
            }
        }
    }
}
